<template>
  <el-card class="sku-list">
    <el-table
      border
      stripe
      :data="skuList"
      v-loading="loading"
      row-key="id">
      <el-table-column
        type="index"
        label="序号"
        width="80"
        align="center" />

      <el-table-column prop="skuName" label="名称"/>

      <el-table-column prop="skuDesc" label="描述"/>

      <el-table-column label="默认图片" width="150" align="center">
        <template slot-scope="{row}">
          <el-image :src="row.skuDefaultImg" alt="商品图片" style="width:80px; height:80px" />
        </template>
      </el-table-column>

      <el-table-column prop="weight" label="重量(KG)" />

      <el-table-column prop="price" label="价格(元)"/>

      <el-table-column label="操作" width="250" align="center">
        <template slot-scope="{row}">
          <HintButton v-if="row.isSale==0" title="上架" type="info" size="mini"
            icon="el-icon-top" @click="onSale(row.id)" />
          <HintButton v-else title="下架" type="success"size="mini"
            icon="el-icon-bottom" @click="cancelSale(row.id)" />
          <HintButton title="修改" type="primary" size="mini" icon="el-icon-edit"
            @click="toUpdateSku"/>
          <HintButton title="查看详情" type="info" size="mini" icon="el-icon-info"
            @click="showSkuInfo(row.id)"/>
          <el-popconfirm :title="`确定删除${row.skuName}吗`" @onConfirm="deleteSku(row.id)">
            <hint-button style="margin-left:10px" slot="reference" type="danger" size="mini"
              icon="el-icon-delete" title="删除"></hint-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      :current-page="page"
      :total="total"
      :page-size="limit"
      :page-sizes="[5,10,20,30,40,50]"
      style="padding: 20px 0; text-align: center;"
      layout="prev, pager, next, jumper, ->, sizes, total"
      @current-change="getSkuList"
      @size-change="changeSize"/>

    <el-drawer
      :visible.sync="isShowSkuInfo"
      :with-header="false"
      size="50%">
      <el-row>
        <el-col :span="5">名称</el-col>
        <el-col :span="16">{{skuInfo.skuName}}</el-col>
      </el-row>
      <el-row>
        <el-col :span="5">描述</el-col>
        <el-col :span="16">{{skuInfo.skuDesc}}</el-col>
      </el-row>
      <el-row>
        <el-col :span="5">价格</el-col>
        <el-col :span="16">{{skuInfo.price}}</el-col>
      </el-row>

      <el-row>
        <el-col :span="5">平台属性</el-col>
        <el-col :span="18">
          <!-- v-for与v-if一起有什么问题? -->
          <div v-if="skuInfo.skuAttrValueList && skuInfo.skuAttrValueList.length>0">
            <el-tag type="success" style="margin-right: 5px" 
              v-for="value in skuInfo.skuAttrValueList" :key="value.id">
                {{value.attrName + '-' + value.valueName}}
            </el-tag>
          </div>
          <div v-else>无</div>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="5">销售属性</el-col>
        <el-col :span="18">
          <div v-if="skuInfo.skuSaleAttrValueList && skuInfo.skuSaleAttrValueList.length>0">
            <el-tag type="success" style="margin-right: 5px" v-for="value in skuInfo.skuSaleAttrValueList" :key="value.id">
              {{value.id + '-' + value.saleAttrValueId}}
            </el-tag>
          </div>
          <div v-else>无</div>
        </el-col>
      </el-row>

      <el-row>
        <el-col :span="5">商品图片</el-col>
        <el-col :span="16">
           <el-carousel :autoplay="false" trigger="click" height="400px">
            <el-carousel-item v-for="item in skuInfo.skuImageList" :key="item.id">
              <img
                style="width: 100%; height: 100%;"
                :src="item.imgUrl"/>
            </el-carousel-item>
          </el-carousel>
        </el-col>
      </el-row>
    </el-drawer>
  </el-card>
</template>

<script>
export default {
  name: 'SkuList',

  data(){
    return{
      loading: false,//是否正在加载中
      skuList: [],//SKU列表
      total: 0,//数据库中的总记录数
      page: 1,//默认页码
      limit: 10,//每页记录数  pageSize
      
      isShowSkuInfo: false, // 是否显示sku详情
      skuInfo: {}, // sku详情对象
    }
  },

  mounted(){
    this.getSkuList()
  },

  methods:{
    /* 
    进入sku修改界面
    */
    toUpdateSku(){
      this.$message.info('正在开发中...')
    },

    /*
    异步获取指定页码的sku列表
    */
    async getSkuList(page=1){
      this.page = page
      this.loading = true
      const result = await this.$API.sku.getList(this.page,this.limit)
      const {records,total} = result.data
      this.skuList = records
      this.total = total
      this.loading = false
    },

    /*
    当页码发生改变自动调用
    */
    changeSize(size){
      this.limit=size
      this.getSkuList(1)
    },

    /*
    删除SKU
    */
    async deleteSku(skuId){
      await this.$API.sku.remove(skuId)
      this.$message.success('删除SKU成功')
      this.getSkuList(1)
    },

    /*
    对指定SKU进行上架的请求
    */
    onSale(skuId) {
      this.$API.sku.onSale(skuId).then(result => {
        this.$message({
          type: 'success',
          message: '上架成功!'
        })
        this.getSkuList(this.page)
      })
    },

    /*
    对指定SKU进行下架的请求
    */
    cancelSale(skuId) {
      this.$API.sku.cancelSale(skuId).then(result => {
        this.$message({
          type: 'success',
          message: '下架成功!'
        })
        this.getSkuList(this.page)
      })
    },

    /* 
    显示sku详情
    */
    async showSkuInfo (id) {
      // 显示drawer
      this.isShowSkuInfo = true
      // 发请求获取sku详情
      const result = await this.$API.sku.get(id)
      // 保存到data显示
      this.skuInfo = result.data
    },
  }
}
</script>

<style lang="scss">
/* 方式一: 不使用scoped样式 */
  .sku-list {
    .el-carousel__container {
      width: 400px;
    }
  }
</style>

<style lang="scss" scoped>
  .sku-list {
    .el-row {
      margin-top: 10px;
      .el-col-5 {
        text-align: right;
        margin-right: 20px;
      }
    }
    /* 方式二: 使用深度作用域选择器 */
    ::v-deep .el-drawer__body { /* 自动形成滚动, 但默认会显示滚动条 */
      overflow: auto;

      &::-webkit-scrollbar { /* 隐藏滚动条 */
        display: none;
      }

      .el-carousel__indicator {  // is-active
        .el-carousel__button { // 指定所有非选中的指示器样式
          background-color: pink;
        }
        &.is-active { // 指定选中的指示器的样式  // & 父级引用
          .el-carousel__button {
            background-color: hotpink;
          }
        }
      }
    }
  }
</style>

